package com.crm.service.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.dao.BaseMapper;
import com.crm.model.BaseModel;
import com.crm.model.SysUser;
import com.crm.service.BaseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


 /**  
 * ClassName: BaseServiceImpl <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason: TODO ADD REASON(可选). <br/>  
 * date: 2016年10月12日 上午11:17:59 <br/>  
 *  
 * @author hanlinxiu  
 * @version @param <T>  
 * @since JDK 1.8  
 */
@Service
public abstract class BaseServiceImpl<T extends BaseModel> implements BaseService<T>{
	
	@Autowired
	BaseMapper<T> baseMapper;
	
	@Autowired  
	private HttpSession session;  

	@Override
	public int deleteByPrimaryKey(String id) {
		return baseMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(T record) {
		
		SysUser user = new SysUser();
		user = (SysUser)session.getAttribute("currentUser");
		
		Date currentDate = new Date();
		
		record.setCreateDate(currentDate);
		record.setCreateUserId(user.getUserId());
		record.setCreateUserName(user.getRealName());
		//record.setDeleteMark(1);
		record.setEnable(1);
		record.setModifyDate(currentDate);
		record.setModifyUserId(user.getUserId());
		record.setModifyUserName(user.getRealName());
		
		return baseMapper.insert(record);
	}

	@Override
	public T selectByPrimaryKey(String id) {
		return baseMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<T> selectByUs(T t) {
		return baseMapper.selectByUs(t);
	}
	
	@Override
	public List<T> selectAll(Page<T> page) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "create_date desc");
		
		List<T> list = baseMapper.selectAll();
		
		return list;
	}
	
	public List<T> selectAll(Page<T> page,T t) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "create_date desc");
		
		List<T> list = baseMapper.selectAllByWhere(t);
		
		return list;
	}

	@Override
	public int updateByPrimaryKey(T record) {
		
		SysUser user = new SysUser();
		user = (SysUser)session.getAttribute("currentUser");
		
		record.setModifyDate(new Date());
		record.setModifyUserId(user.getUserId());
		record.setModifyUserName(user.getRealName());
		
		return baseMapper.updateByPrimaryKey(record);
	}
	
}
